Public Class FormInsuranceAED
    Public empId, InId As Int16
    Dim Ins As New InsuranceInfo
    Dim Insurance As New InsuranceInfo
    Private Sub FormInsuranceAED_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

    Public Sub New()

        InitializeComponent()
        ClearText()
        Dbinterface.OpenConnection()
        LoadName()
        LoaUser()
    End Sub
    Public Sub ClearText()

        For Each Clear As Control In GroupBox1.Controls
            If TypeOf Clear Is TextBox Then
                Clear.Text = ""
            End If
        Next
    End Sub

    Private Sub SaveUpdate()
        Try
            'Passing values from object in class to TextBox
            With Insurance
                .InsuranceId = InId
                .EmployeeId = empId
                .OccurDate = txtOccurDate.Text
                .OpenClaim = txtOpenClaim.Text
                .TypeOfClaim = txtTypeOfClaim.Text
                .UserId = userId
                .Amount = txtAmount.Text
                .Description = txtNote.Text
                .Company = txtCompany.Text
                .Active = True

                If InId = Nothing Then
                    'Insert familycation

                    Insurances.AddInsurance(Insurance)
                Else
                    'Update familycation

                    Insurances.UpdateInsurance(Insurance)

                End If

            End With
            AddInseranceToDataGridView()

        Catch ex As Exception
            MsgBox(ex.Message)

        End Try
        Me.Close()
    End Sub
    Public Sub AddInseranceToDataGridView()
        Try
            'Check database connection
            Dim cn = Dbinterface.Connection
            If cn.State = ConnectionState.Closed Then

                Exit Sub

            End If
            'Passing vlues DataGridView in form familycation
            With FormInsurance.dgvInsurance.CurrentRow
                .Cells("EMPLOYEE_NAME").Value = cboEmp_Name.Text
                .Cells("OCCUR_DATE").Value = Insurance.OccurDate
                .Cells("OPEN_CLAIM").Value = Insurance.OpenClaim
                .Cells("TYPE_OF_CLAIM").Value = Insurance.TypeOfClaim
                .Cells("AMOUNT").Value = Insurance.Amount
                .Cells("MODIFY_BY").Value = cboUserId.Text
                .Cells("DESCRIPTION").Value = Insurance.Description
                .Cells("COMPANY").Value = Insurance.Company
                If InId = Nothing Then

                    .Cells("INSURANCE_ID").Value = Insurance.InsuranceId
                    RefreshData.DescendingRefresh(FormInsurance.dgvInsurance, 0)

                    FormInsurance.dgvInsurance.CurrentCell = FormInsurance.dgvInsurance.Rows(0).Cells(1)
                Else

                    FormInsurance.dgvInsurance.CurrentCell = FormInsurance.dgvInsurance.Rows(0).Cells(1)

                    Exit Sub
                End If

            End With
        Catch ex As Exception
            MsgBox(ex.Message)

        End Try
    End Sub

    Private Sub cboEmp_Name_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboEmp_Name.SelectedIndexChanged
        Dim group As EmployeeInfo = CType(cboEmp_Name.SelectedItem, EmployeeInfo)
        empId = group.EmployeeId
    End Sub
    Private Sub LoadName()
        For Each obj As Object In GetEmployeeName.GetEmployee.Values
            cboEmp_Name.Items.Add(obj)
        Next
    End Sub
    Private Sub LoaUser()
        For Each obj As Object In Users.getUser.Values
            cboUserId.Items.Add(obj)
        Next
    End Sub
    Public userId As Int16
    Private Sub cboUserId_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboUserId.SelectedIndexChanged
        Dim group As UserInfo = CType(cboUserId.SelectedItem, UserInfo)
         userId = group.UserId
    End Sub

    Private Sub btnSaveUpdate_Click(sender As Object, e As EventArgs) Handles btnSaveUpdate.Click
        SaveUpdate()
    End Sub

    Private Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub
End Class